import notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import utils from '../../utils/util.js'

const db = wx.cloud.database();
const date = new Date()
const app = getApp()
Page({
  data: {
    show_user: false,
    show_appointment: false,
    show_seat: false,
    show_notify:  false,
    current_seat: null,
    show: false,
    tab: ["二楼", "三楼", "四楼", "五楼"],
    seat_position_form:[],
    appointment_form: [],
    user_form: [],
    floor: '二楼',
    seat_add:{
        seat_position: null,
        seat_message: ''
    },
    notify:{
      title: '',
      message: '',
      time: '',
      name:  ''
    }
  },
  onChangeTitle(e){
   
    this.setData({
      'notify.title':e.detail
    })
  },
  onChangMessage(e){
    this.setData({
      'notify.message': e.detail
    })
  },
  showNotice(){
    this.setData({
      show_notify: true
    })

  },
  sendNotify(){
    let time = utils.formatTime(new Date())
    this.setData({
      'notify.time':  time
    })
    db.collection("notifyMessage").add({
      data: this.data.notify
    }).then(res=>{

      db.collection("notifyMessage").doc('notifyHeader').update({
        data: {
          message: '管理员发布了一条新消息'
        }
      })

      Toast.success('发布成功');
      this.setData({
        show_notify: false
      })
    })

  },
  //显示用户管理界面
  async user_manage(){
    this.setData({user_form:[]})
    // db.collection("user").get().then(res=>{
    //   console.log(res)
    //   this.setData({
    //       user_form: res.data
    //     })
    // })

  let count_user = await db.collection("user").count()
  count_user = count_user.total
   let all_user = []
   for (let i = 0; i < count_user; i += 20){
     let list_user =  await db.collection("user").skip(i).get()
      all_user = all_user.concat(list_user.data)
   }
   this.setData({
    user_form: all_user
  })
    this.setData({
      show_user: true
    })
  },
  close_user_manager(){
    this.setData({
      show_user: false
    })
  },
  //切换楼层
  onChange(event) {
    this.setData({
        floor:event.detail.title
    })
    wx.showToast({
      title: `切换 ${event.detail.title}`,
      icon: 'none',
    });
    this.onLoad()
  },
  onColse(){
    this.setData({show_appointment: false})
  },
  //展示预约列表
  check_all_appointment(){
    this.setData({
        show_appointment: true
    })
  },
  //删除预约信息
  delete_appointment(e){
    db.collection("appointment_time").where({
        'floor': this.data.floor,
        '_id': e.currentTarget.id
    }).remove().then(res=>{
        Toast.success('删除成功');
        this.onLoad()
    })
  },
  //获取添加的座位号
  seat_position_change(e){
    this.setData({
        ['seat_add.seat_position']: e.detail.value
    })
  },
  //更改座位描述信息
  seat_message_change(e){
    this.setData({
        ['seat_add.seat_message']: e.detail.value
    })
  },
  //取消添加座位
  cancel(){
    this.setData({ show: false, show_seat: false ,show_notify:false});
  },
  //确认添加座位
  confirm(){
    // db.collection("seat_position_form").where
    let seat_repeat = false
    for(let i = 0; i < this.data.seat_position_form.length; i++){
        if(this.data.seat_add.seat_position===this.data.seat_position_form[i].position){
            seat_repeat = true
        }
    }
    if(seat_repeat){
        Toast.fail('座位重复');
    }else{
        db.collection("seat_position_form").add({
            data:{
                position: this.data.seat_add.seat_position,
                message:  this.data.seat_add.seat_message,
                floor: this.data.floor
            }
        }).then(res=>{
            Toast.success('添加成功');
            this.onLoad()
        })
    }
    this.setData({ show: false });
  },
  //展示添加座位界面
  add_seat(){
    this.setData({ show: true });
  },
  //展示座位详情,更改，删除
  mySeat(e){

    this.setData({ show_seat: true, current_seat: e.currentTarget.id});
  },
  //删除座位
  delete_seat(){
    db.collection("seat_position_form").where({
        'floor': this.data.floor,
        'position': this.data.current_seat
    }).remove().then(res=>{
        Toast.success('删除成功');
        this.onLoad()
    })
    this.setData({
        show_seat: false
    })
  },
  onLoad: async function (options) {  
    //拉取座位信息

    
    // db.collection("seat_position_form").where({
    //     "floor": this.data.floor
    // }).get().then(res=>{
    //     this.setData({
    //         seat_position_form: res.data
    //     })
    // })

     //获取所有座位信息
     let count_seat = await db.collection("seat_position_form").count()
     count_seat = count_seat.total
     let all_seat = []
     for (let i = 0; i < count_seat; i+= 20){
       let  list_seat = await db.collection("seat_position_form").where({
         floor: this.data.floor
        }).skip(i).get()
        all_seat = all_seat.concat(list_seat.data)
     }
     this.setData({
      seat_position_form: all_seat
     })
     
   //获取所有预约信息
   let count_app = await db.collection("appointment_time").count()
   count_app = count_app.total
   let all_app = []
   for (let i = 0; i < count_app; i += 20){
     let list_app =  await db.collection("appointment_time").where({
       floor: this.data.floor
      }).skip(i).get()
     all_app = all_app.concat(list_app.data)
   }

   let show = []; 
   let min_s
   let min_e
   for (let i = 0; i < all_app.length; i++){
       let s = new Date(all_app[i].start_time)
       let e = new Date(all_app[i].end_time)
       let day_ = '';
       if(s.getDate()==date.getDate()){ 
         day_ = '今天'
       }else{
         day_ = '明天'
       }
       let min_s = s.getMinutes()
       let min_e = e.getMinutes()
       if(s.getMinutes()<10){
           min_s = '0'+ s.getMinutes()
       }
       if(e.getMinutes()<10){
           min_e = '0'+ e.getMinutes()
       }

       let seat_ = {day: day_, start_time: s.getHours()+':'+min_s,end_time: e.getHours()+':'+min_e,name:all_app[i].name, status:all_app[i].status, _id: all_app[i]._id }   
       show.push(seat_)  
   }
   this.setData({
      'notify.name': app.globalData.name,
      appointment_form: show
   })


  
    // //拉取预约信息并格式转换
    // db.collection("appointment_time").where({
    //     "floor": this.data.floor 
    // }).get().then(res=>{
    //     //格式转换
    //     let show = []; 
    //     let min_s
    //     let min_e
    //     for (let i = 0; i < res.data.length; i++){
    //         let s = new Date(res.data[i].start_time)
    //         let e = new Date(res.data[i].end_time)
    //         let day_ = '';
    //         if(s.getDate()==date.getDate()){ 
    //           day_ = '今天'
    //         }else{
    //           day_ = '明天'
    //         }
    //         let min_s = s.getMinutes()
    //         let min_e = e.getMinutes()
    //         if(s.getMinutes()<10){
    //             min_s = '0'+ s.getMinutes()
    //         }
    //         if(e.getMinutes()<10){
    //             min_e = '0'+ e.getMinutes()
    //         }

    //         let seat_ = {day: day_, start_time: s.getHours()+':'+min_s,end_time: e.getHours()+':'+min_e,name:res.data[i].name, status:res.data[i].status, _id: res.data[i]._id }   
    //         show.push(seat_)  
    //     }
    //     this.setData({
    //         appointment_form: show
    //     })
    // })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})